using System;
using UnityEngine;
using QFramework;

namespace Survivor
{
	public partial class Circle : ViewController
	{
		public Transform center;
		public float radius = 3;
		private float _angle = 0;
		public float Angle
		{
			get => _angle;
			set => _angle = value % 360;
		}

		void Start()
		{
			this.OnTriggerEnter2DEvent((collider) =>
			{
				var enemy = collider.GetComponentInParent<Enemy>();
				if (enemy)
				{
					enemy.Hurt(Global.SimpleCircleDamage.Value);
				}
			}).UnRegisterWhenGameObjectDestroyed(this);
			
			
			Global.SimpleCircleScale.RegisterWithInitValue((value) =>
			{
				this.transform.localScale =Vector3.one * value;
			}).UnRegisterWhenGameObjectDestroyed(this);
		}

		public void Init(Transform roundCenter, float initAngle)
		{
			this.center = roundCenter;
			this.Angle = initAngle;
		}

		private void Update()
		{
			Angle += Global.SimpleCircleSpinSpeed.Value * Time.deltaTime;
			transform.position =center.position + new Vector3(radius * Mathf.Cos(Angle *Mathf.Deg2Rad), radius * Mathf.Sin(Angle *Mathf.Deg2Rad));
			transform.rotation = Quaternion.Euler(0,0,-(90-Angle));
		}
	}
}
